﻿using System;
using System.Windows;
using System.Windows.Controls;
using TS3ServerManager.BL;

namespace TS3ServerManager.Controls
{
    /// <summary>
    /// Interaction logic for ServerModifyControl.xaml
    /// </summary>
    public partial class ServerModifyControl : UserControl
    {
        #region Properties

        public static readonly DependencyProperty IsServerNameVisibleProperty = DependencyProperty.Register("IsServerNameVisible", typeof(bool), typeof(ServerModifyControl), new FrameworkPropertyMetadata(false));

        public bool IsServerNameVisible
        {
            get { return (bool)GetValue(IsServerNameVisibleProperty); }
            set { SetValue(IsServerNameVisibleProperty, value); }
        }

        public string ServerName
        {
            get { return NameTextBox.Text.Trim(); }
            set { NameTextBox.Text = value; }
        }

        #endregion

        #region Constructor

        public ServerModifyControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Public Methods

		public void Bind()
		{
			Bind(null, null);
		}

        public void Bind(string serverName, ServerConnectInfo connectInfo)
        {
            ServerName = serverName;
            HostTextBox.Text = connectInfo == null ? null : connectInfo.Host;
            QueryPortTextBox.Text = connectInfo == null ? null : Convert.ToString(connectInfo.QueryPort);
            VoicePortTextBox.Text = connectInfo == null ? null : Convert.ToString(connectInfo.VoicePort);
            LoginCheckBox.IsChecked = connectInfo == null ? false : !connectInfo.Username.IsNullOrTrimmedEmpty() && !connectInfo.Password.IsNullOrTrimmedEmpty();
            UsernameTextBox.Text = connectInfo == null ? null : connectInfo.Username;
            PasswordTextBox.Password = connectInfo == null ? null : connectInfo.Password;
        }

        public ServerConnectInfo ValidateUserInterface()
        {
            string host = HostTextBox.Text.Trim();

            if (IsServerNameVisible && ServerName.IsNullOrTrimmedEmpty())
            {
                MessageBox.Show("Please provide a name for the server!");
                return null;
            }

            if (host.IsNullOrTrimmedEmpty())
            {
                MessageBox.Show("Please provide a host to connect to!");
                return null;
            }

            ushort queryPort;

            if (!ushort.TryParse(QueryPortTextBox.Text.Trim(), out queryPort))
            {
                MessageBox.Show("Please provide a valid query port!");
                return null;
            }

            ushort? voicePort = null;

            if (!VoicePortTextBox.Text.IsNullOrTrimmedEmpty())
            {
                ushort voicePortTemp;
                if (ushort.TryParse(VoicePortTextBox.Text.Trim(), out voicePortTemp))
                    voicePort = voicePortTemp;
                else
                {
                    MessageBox.Show("Please provide a valid query port!");
                    return null;
                }
            }

            string username = null;
            string password = null;

            if (LoginCheckBox.IsChecked == true)
            {
                if (UsernameTextBox.Text.IsNullOrTrimmedEmpty())
                {
                    MessageBox.Show("Please provide a non empty username or deselect the login checkbox!");
                    return null;
                }

                if (PasswordTextBox.Password.IsNullOrTrimmedEmpty())
                {
                    MessageBox.Show("Please provide a non empty password or deselect the login checkbox!");
                    return null;
                }

                username = UsernameTextBox.Text.Trim();
                password = PasswordTextBox.Password.Trim();
            }

            return new ServerConnectInfo(host, queryPort, voicePort, username, password);
        }

        #endregion
    }
}